﻿#$language = "Python"
#$interface = "1.0"

import re

def main():
    orginNmae = crt.Dialog.Prompt("Enter orgin strings:", "Replace dialog")
    if orginNmae == "":
        crt.Screen.Send("echo 'No strings input for orgin!'\r")
        return 1
    orginNmae = re.escape(orginNmae)

    replaceNmae = crt.Dialog.Prompt("Enter new strings:", "Replace dialog")
    if replaceNmae == "":
        result = crt.Dialog.MessageBox("Please comfirm new string is empty?", "Replace dialog", ICON_QUESTION | BUTTON_YESNO )
        if result == IDNO:
            return 2
    else:
        replaceNmae = re.escape(replaceNmae)

    cmd_part_1 = "find | sed 's/\\.\\///g' | grep \"" + orginNmae + "\" "
    cmd_part_2 = "| sed 's/\\(.*\\)" + orginNmae + "\\(.*\\)/mv \\\"&\\\" \\\"\\1" + replaceNmae + "\\2\\\"/g' "
    crt.Screen.Send(cmd_part_1 + cmd_part_2 + " | sed -n '1p' \r")
    crt.Screen.WaitForString("!justwait", 1)

    result = crt.Dialog.MessageBox("Are you want to going?", "Replace dialog", ICON_QUESTION | BUTTON_YESNO )
    if result == IDNO:
        return 2
    if result == IDYES:
        crt.Screen.Send(cmd_part_1 + cmd_part_2 + " \r")
        crt.Screen.Send(cmd_part_1 + cmd_part_2 + " | sh \r")
        return 0
    return 0

crt.Screen.Synchronous = True
main()
crt.Screen.Synchronous = False